import 'dart:ui' as ui;
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_logs/flutter_logs.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:meiyunapp/component/not_find_page.dart';
import 'package:meiyunapp/net/api/goods_http.dart';
import 'package:meiyunapp/router/routers_pages.dart';
import 'package:meiyunapp/service/db_services.dart';
import 'package:meiyunapp/service/mqtt_services.dart';
import 'package:meiyunapp/service/store_services.dart';
import 'package:meiyunapp/style/app_theme.dart';
import 'package:meiyunapp/style/k_color.dart';
import 'package:meiyunapp/utils/flutter_logs_utils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'generated/locales.g.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.light,
      statusBarIconBrightness: Brightness.dark,
      systemNavigationBarDividerColor: Colors.transparent,
      systemNavigationBarColor: Colors.white,
      systemNavigationBarIconBrightness: Brightness.dark,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
  await initServices();
  runApp(const MyApp());

  initLogs();
}

///初始化service
Future<void> initServices() async {
  await Get.putAsync(() => StoreService().init());
  await Get.putAsync(() => MqttServices().init());
  Get.put(DBService());
}

void initLogs() async {
  await FlutterLogs.initLogs(
      logLevelsEnabled: [
        LogLevel.INFO,
        LogLevel.WARNING,
        LogLevel.ERROR,
        LogLevel.SEVERE
      ],
      timeStampFormat: TimeStampFormat.TIME_FORMAT_READABLE,
      directoryStructure: DirectoryStructure.FOR_DATE,
      logTypesEnabled: ["device", "network", "errors"],
      logFileExtension: LogFileExtension.LOG,
      logsWriteDirectoryName: "MyLogs",
      logsExportDirectoryName: "MyLogs/Exported",
      debugFileOperations: true,
      isDebuggable: true);

  LogsUtils.uploadFile();
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return RefreshConfiguration(
      headerBuilder: () => const WaterDropHeader(),
      footerBuilder: () => const ClassicFooter(),
      headerTriggerDistance: 80.0,
      maxOverScrollExtent: 100,
      hideFooterWhenNotFull: true,
      enableBallisticLoad: true,
      child: GetMaterialApp(
        onInit: () {
          Get.put(GoodsHttp(), permanent: true);
        },
        title: LocaleKeys.app_name.tr,
        /*theme: ThemeData(
          primarySwatch: KColor.MColor0C5BE5,
          textButtonTheme: TextButtonThemeData(
            style: TextButton.styleFrom(primary: KColor.Color0C5BE5),
          ),
          textTheme: TextTheme(
            headline5: TextStyle(fontSize: 16.sp, color: KColor.Color333333),
            headline6: TextStyle(fontSize: 14.sp, color: KColor.Color777777),
            overline: TextStyle(fontSize: 12.sp, color: KColor.ColorAAAAAA),
          ),
        ),*/
        /*theme: AppTheme.light.copyWith(
          textButtonTheme: TextButtonThemeData(
            style: TextButton.styleFrom(primary: KColor.Color0C5BE5),
          ),
          textTheme: TextTheme(
            headline5: TextStyle(fontSize: 16.sp, color: KColor.Color333333),
            headline6: TextStyle(fontSize: 14.sp, color: KColor.Color777777),
            overline: TextStyle(fontSize: 12.sp, color: KColor.ColorAAAAAA),
          ),
        ),*/
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        supportedLocales: const [
          Locale('en', 'US'),
          Locale('zh', 'CN'),
          Locale('ar', 'OM'),
        ],
        localeResolutionCallback:
            (Locale? locale, Iterable<Locale> supportedLocales) {
          return Locale('zh', 'CN');
        },
        getPages: RoutersPages.pages,
        initialRoute: RouterPath.splash_path,
        unknownRoute: GetPage(
          name: RouterPath.page_404,
          page: () => const NotFindPage(),
        ),
        locale: ui.window.locale,
        //locale: Locale('ar', 'OM'),
        enableLog: true,
        translationsKeys: AppTranslation.translations,
        logWriterCallback: (text, {bool isError = false}) {
          LogsUtils.loginfo("GETX", "log", text);
        },
        builder: (context, widget) {
          return MediaQuery(
            ///设置文字大小不随系统设置改变
            data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
            child: widget ?? Container(),
          );
        },
      ),
    );
  }
}
